package com.jie.lanxin.impl;

import com.jie.constants.LanXinModelConstants;
import com.jie.dto.CombinationTopicDTO;
import com.jie.exception.BizException;
import com.jie.lanxin.dto.MbeddingDTO;
import com.jie.lanxin.service.LanXinService;
import com.jie.lanxin.vo.MbeddingUpdateVO;
import com.jie.lanxin.vo.MbeddingVO;
import com.jie.mapper.ThematicCategoryMapper;
import com.jie.utils.RestTemplateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;

import static com.jie.constants.LanXinModelConstants.MBEDDING_PAGE_SIZE;
import static com.jie.constants.LanXinModelConstants.MBEDDING_REDIS_KEY;

/**
 * @Author: 杰
 * @Project :net-disk
 * @DateTime: 2024/5/12 12:59
 **/
@Service
public class LanXinServiceImpl implements LanXinService {
    @Value("${lanxin.url}")
    private String url;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ThematicCategoryMapper thematicCategoryMapper;

    /**
     * 根据用户输入内容推荐专题
     * @param mbeddingVO
     * @return
     */
    @Override
    public List<CombinationTopicDTO> getMbeddingThematicCategory(MbeddingVO mbeddingVO) {
        //获取url
        String urlChatGpt=url+ LanXinModelConstants.MBEDDING_URL;
        //获取
        try {
            ResponseEntity<MbeddingDTO> post = RestTemplateUtils.post(urlChatGpt, mbeddingVO, MbeddingDTO.class);
            //获取到post.getBody()
            MbeddingDTO body = post.getBody();
            //根据body的返回信息查询专题数据库推荐。
            List<String> message = body.getMessage();
            StringBuilder str=new StringBuilder();
            message.forEach(str::append);
            //根据message查询redis
            List<CombinationTopicDTO> res= (List<CombinationTopicDTO>) redisTemplate.opsForValue().get(MBEDDING_REDIS_KEY+str);
            if (Objects.isNull(res)){
                //根据当前message专题信息返回全部运动视频
                res=thematicCategoryMapper.selectTopTopicsByClassNames(message,MBEDDING_PAGE_SIZE);
                //存入redis
                redisTemplate.opsForValue().set(MBEDDING_REDIS_KEY+ str,res);
            }
            return res;
        }catch (Exception e){
            throw new BizException(e.getMessage());
        }
    }

    /**
     * 更新推荐系统
     * @return
     */
    @Override
    public String updateMbeddingModel() {
        //获取url
        String urlChatGpt=url+ LanXinModelConstants.MBEDDING_UPDATE_URL;
        //获取全部专题信息
        List<String> thematicCategoryName = thematicCategoryMapper.getThematicCategoryName();
        MbeddingUpdateVO mbeddingUpdateVO = MbeddingUpdateVO.
                builder().message(thematicCategoryName).build();
        try {
            ResponseEntity<String> post = RestTemplateUtils.post(urlChatGpt, mbeddingUpdateVO, String.class);
            return post.getBody();
        }catch (Exception e){
            throw new BizException(e.getMessage());
        }
    }
}
